
import com.gxa.mapper.EmployeeMapper;
import com.gxa.mapper.EventMapper;
import com.gxa.pojo.Event;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 赵贺益
 * Date: 2021/11/18 18:34
 * Description:
 * Version: V1.0
 */
public class Mybatis02 {
    SqlSession sqlSession;
    @Before
    public  void before() throws IOException {
//        读取的mybatis核心配置文件
        String resource = "Mybatis.xml";
//        通过流的方式读取核心配置文件，把它加载到内存中
        InputStream inputStream = Resources.getResourceAsStream(resource);
//        创建SqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//        通过工厂对象调用获得SqlSession对象  //默认是手动提交；如果要改变为自动提交，则设置为true表示自动提交
        sqlSession=sqlSessionFactory.openSession(true);
    }

    /**
     * 以map为参数向event数据表中插入数据
     */
    @Test
    public void insertMap(){
        EventMapper eventMapper=sqlSession.getMapper(EventMapper.class);
        Map map=new HashMap();
        map.put("et_no",123458);
        map.put("et_typename","赵贺益");
        map.put("et_descript","长得很帅");
        map.put("et_state",1);
        int result= eventMapper.inseEventtMap(map);
        System.out.println(result);
    }

    /**
     *根据et_no查询出一个对象
     */
    @Test
    public void selectEvent(){
        Map map=new HashMap();
        map.put("et_no",123458);
        EventMapper eventMapper=sqlSession.getMapper(EventMapper.class);
        Event event= eventMapper.selectEvent(map);
        System.out.println(event);
    }

    /**\
     * 返回一个List<Event>集合
     */
    @Test
    public  void  selectListEvent() {
        EventMapper eventMapper = sqlSession.getMapper(EventMapper.class);
        List<Event> eventList = eventMapper.selectListEvent("赵贺益");
        System.out.println(eventList);
    }

    /**\
     * 返回一个List<Map>
     */
    @Test
    public  void  selectMapEvent() {
        EventMapper eventMapper = sqlSession.getMapper(EventMapper.class);
        List<Map> eventListMap = eventMapper.selectListMapEvent("赵贺益");
        System.out.println(eventListMap);
    }
}
